﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Workflow2211A.Domain.Entities;
using Workflow2211A.Domain;
using WorkflowCore.Interface;
using WorkflowCore.Models;

namespace Workflow2211A.Application.Steps
{
    /// <summary>
    /// 结束步骤
    /// </summary>
    public class EndStep : StepBody
    {
        private readonly IBaseRepository<WorkflowInstanceModel> _instanceRepository;
        public EndStep(IBaseRepository<WorkflowInstanceModel> instanceRepository)
        {
            _instanceRepository = instanceRepository;
        }
        public override ExecutionResult Run(IStepExecutionContext context)
        {
            Console.WriteLine("步骤结束！！！");
            //手动的将流程设置为结束,（在一些场景下会影响到工作流的自动结束）
            context.Workflow.Status = WorkflowStatus.Complete;

            var instance = _instanceRepository.GetAll().FirstOrDefault(x => x.WorkflowId == context.Workflow.Id);
            instance.StepId = context.Step.ExternalId;
            instance.StepName = context.Step.Name;
            instance.Status = "结束";
            instance.AuditUserId = "";
            instance.AuditUserName = "";
            instance.CompleteTime = DateTime.Now;
            _instanceRepository.Update(instance);
            return ExecutionResult.Next();
        }
    }
}
